package com.enthal.basicpers

import android.util.Log
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.IOException

object HttpRequest {
    private val client = OkHttpClient()
    private val MEDIA_TYPE_JSON = "application/json; charset=utf-8".toMediaType()

    fun getFilters() {
        val postBody = """
        {"device_id": "abcdef"}
        """.trimMargin()

        val request = Request.Builder()
            .url("http://47.57.112.157:8963/bitpay/app/filter")
            .post(postBody.toRequestBody(MEDIA_TYPE_JSON))
            .build()

        try {
            client.newCall(request).execute().use { response ->
                if (!response.isSuccessful) throw IOException("Unexpected code $response")

                println(response.body!!.string())
            }
        } catch (e: Exception) {
            Log.e("NetError", e.message)
        }
    }
}